package com.yang.studentsapp.utils;


import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.yang.studentsapp.ProtocolKey;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/10/11.
 * 泛型T 指的是 想要获取的数据的类型
 */
public class MyRequest<T> extends Request<JsonElement> {
    private TypeToken<T> mTypeToken;
    private IRequest<T> mIResult;
    public MyRequest(int method, String url, TypeToken<T> token, IRequest<T> iRequest) {
        this(method, url, null, token, iRequest);
    }

    public MyRequest(int method, String url, Map<String,String> params, TypeToken<T> token, IRequest<T> iRequest) {
        super(method, url, null);
        mTypeToken = token;
        mIResult = iRequest;
        if (params == null || params.isEmpty()) {
            return;
        }
        try {
            getParams().putAll(params);
        } catch (AuthFailureError authFailureError) {
            authFailureError.printStackTrace();
        }

    }

    @Override
    protected Response<JsonElement> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers, "utf-8"));
            JsonElement element = new JsonParser().parse(jsonString);

            return Response.success(element,
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        }
    }

    @Override
    protected void deliverResponse(JsonElement response) {
        JsonObject jsonObject = response.getAsJsonObject();
        JsonElement resultElement = jsonObject.get(ProtocolKey.status);
        int resultCode = resultElement.getAsInt();
        switch (resultCode) {
            case ProtocolKey.SUCCESS:
                T t = JsonUtils.parseArray(jsonObject.get(ProtocolKey.data),mTypeToken);
                mIResult.success(t);
                break;
            case ProtocolKey.fail:
                String msg = jsonObject.get(ProtocolKey.msg).getAsString();
                mIResult.fail(msg);
                break;
        }

    }

    @Override
    public void deliverError(VolleyError error) {
        super.deliverError(error);
        mIResult.error(error.getMessage());
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return new HashMap<>();
    }
    public interface IRequest<T>  {
        void fail(String msg);
        void success(T t);
        void error(String msg);
    }
}
